/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaMappingServiceVersion;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class MappingServiceVersionDao {

	public static void createVersion(List s) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();

			DaoManager.getDao().saveOrUpdateAll(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.createVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createVersion() error", e);
		} finally {
			tx.end();
		}

	}

	public static void createVersion(SoaMappingServiceVersion s)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaMappingServiceVersion s where s.version = ? and s.id = ?";
			List o = DaoManager.getDao().find(hql, new Object[]{s.getVersion(),s.getId()});
			if(o.size() > 0 ){
				throw new BifangException(BifangErrorCode.VERSION_EXIST_ERROR,"version '"
						+ s.getVersion() + "' exists");
			}
			DaoManager.getDao().save(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger
					.error(
							"MappingServiceVersionDao.createVersion(SoaMappingServiceVersion)",
							e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createVersion(SoaMappingServiceVersion) error", e);
		} finally {
			tx.end();
		}

	}

	public static void modifyVersion(SoaMappingServiceVersion s)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();

			DaoManager.getDao().merge(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger
					.error(
							"MappingServiceVersionDao.modifyVersion(SoaMappingServiceVersion)",
							e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyVersion(SoaMappingServiceVersion) error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeVersionByServiceId(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaMappingServiceVersion s where s.serviceId = ?";
			List ret = DaoManager.getDao().find(hql, id);
			DaoManager.getDao().deleteAll(ret);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.removeVersionByServiceId()",
					e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeVersionByServiceId() error", e);
		} finally {
			tx.end();
		}

	}

	public static List getVersionByServiceId(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaMappingServiceVersion s where s.serviceId = ?";
			List ret = DaoManager.getDao().find(hql, id);
			DaoManager.getDao().flush();
			tx.commit();
			return ret;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.getVersionByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getVersionByServiceId() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeVersion(SoaMappingServiceVersion v)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(v);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.removeVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeVersion() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeVersion(String vId) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			Object o = DaoManager.getDao().get(SoaMappingServiceVersion.class,
					vId);
			DaoManager.getDao().delete(o);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.removeVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeVersion() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static SoaMappingServiceVersion getVersion(String vId) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaMappingServiceVersion o = (SoaMappingServiceVersion)DaoManager.getDao().get(SoaMappingServiceVersion.class,
					vId);
			tx.commit();
			return o;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusServiceRelDao.class);
			logger.error("MappingServiceVersionDao.getVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getVersion() error", e);
		} finally {
			tx.end();
		}

	}

}
